//
// @Auther: by wujehy 
// @Email:wujehy@qq.com
// @Data:2020/9/11
// @Time:3:57 下午
//
//

#include "ModManager.h"
#include "base_types.h"
#include "BaseMod.h"

#include "glog/logging.h"


using namespace GeeJoan;

void ModManager::init()
{
    for (auto m : m_mod_map)
    {
        m.second->init();
    }
}

void ModManager::init_complete(Global_Context *context)
{
    globalContext=context;
    for (auto m : m_mod_map)
    {
        m.second->init_complete( context );
    }
}

bool ModManager::registerModFunc(BaseMod *mod, bool auto_del)
{
    LOGINFO << " register type " << mod->mod_type();
    auto pt = m_mod_map.insert( {mod->mod_type(), mod} );
    if (pt.second == false && auto_del == true)
    {
        delete mod;
    }
    return pt.second;
}

void ModManager::rounte(std::unique_ptr<HanderDataType> hander)
{
    auto find = m_mod_map.find( static_cast<ModTypeEnum>(hander->modtype));
    StatusCodeTypeEnum code = Status_FAIL;
    std::string respose;
    if (find != m_mod_map.end())
    {
        LOGINFO << " find register mod type = " << int( hander->modtype );
        find->second->procMessage( hander.get(), code, respose );
    } else
    {
        LOGINFO << " un register mod type = " << hander->subtype;
    }
    globalContext->networkManager->sendMsg( std::move( hander ), code, respose );
}

ModManager::~ModManager()
{

    for (auto m  : m_mod_map)
    {
        if (m.second)
        {
            delete m.second;
            m.second = nullptr;
        }
    }
}
